package edu.njust.httpmanager.exception;

import edu.njust.httpmanager.common.Result;
import edu.njust.httpmanager.constance.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice   // 该注解表示该类是一个全局异常处理类
public class GlobalExceptionHandler {

    /**
     * 处理自定义异常
     */
    @ExceptionHandler(CustomException.class)
    public Result<Void> customExceptionHandler(CustomException e) {
        log.error("customException: {}", e.getMessage());
        return Result.error(e.getCode(), e.getMessage());
    }

    /**
     * 处理未知异常
     */
    @ExceptionHandler(RuntimeException.class)
    public Result<Void> runtimeExceptionHandler(RuntimeException e) {
        log.error("runtimeException", e);
        return Result.error(ResponseCode.FAIL);
    }

    @ExceptionHandler(ApiException.class)
    public Result<?> apiExceptionHandler(ApiException e) {
        return Result.success(e.getData());
    }
}
